package com.ruoyi.driver.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.driver.domain.LocationData;
import com.ruoyi.driver.service.IDvDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/dir")
public class DvDriverController extends BaseController
{

    @Autowired
    IDvDriverService dvDriverService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedisService redisService;


   /**
   * //司机听单
   * @author 哲
   * @date 2024/7/2 20:13
 * @param locationData
   */

    @PostMapping("/saveLatitudeAndLongitude")
    public void saveLatitudeAndLongitude(@Validated @RequestBody LocationData locationData){

        int maxSize = 6; // 设置列表的最大长度为6

        redisTemplate.opsForZSet().removeRange("GEO",maxSize,-1);


        redisTemplate.opsForGeo().add("GEO",
                new Point(locationData.getLongitude(),locationData.getLatitude()),locationData.getLoginName());
        System.out.println(locationData.getLoginName()+"-"+locationData.getLongitude()+"-"+locationData.getLatitude());

        String username = SecurityUtils.getUsername();
        System.out.println("司机名称："+username);

        //修改状态(听单)
        Integer update = dvDriverService.updateDriver(username);

    }


    
    /**
    * //司机抢单 并修改状态 为 抢单
    * @author 哲
    * @date 2024/7/3 20:22
     * @return com.ruoyi.common.core.web.domain.AjaxResult
    */
    
    @PostMapping("/sim")
    public AjaxResult sim(){

        LocalDateTime currentDateTime = LocalDateTime.now();
        // 格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);

        System.out.println("当前日期时间: " + formattedDateTime);

        String username = SecurityUtils.getUsername();
        System.out.println("司机名称为：: " + username);

        long timeInMilliseconds = LocalDateTime.parse(formattedDateTime, formatter)
                .atZone(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli();


        Map<Object, Object> map = new HashMap<>();
        map.put("username",username);
        map.put("time", DateUtils.getTime());


        redisTemplate.opsForValue().set("RobTime:", JSON.toJSONString(map));


        //修改司机状态
        Integer update = dvDriverService.updateDriverByStatus(username);

        return AjaxResult.success("抢单成功");

    }




}
